<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>浏览商品</title>
    <link rel="stylesheet" href="/js/bootstrap.min.css">
    <script src="/js/jquery-3.2.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var product = $("#product");//商品
            var pageNo = 1;//页数
            var total = null;//最大页数
            var pname = null;
            // var id = null;

            //设置首页
            $("#first").click(function () {
                pageNo = 1;
                show();
            });

            //设置上一页
            $("#prev").click(function () {
                if (pageNo <= 1)
                    return;
                pageNo--;
                show();
            });

            //设置下一页
            $("#next").click(function () {
                if (pageNo >= total)
                    return;
                pageNo++;
                show();
            });

            //设置尾页
            $("#last").click(function () {
                pageNo = total;
                show();
            });


            //点击查询事件
            $("#button").click(function () {
                pname = $("#pname").val();
                pageNo = 1;
                show();
                return false;
            });

            var ord = "id";//通过属性排序
            var order = 1;//排序
            //展示商品信息
            function show() {
                product.find("tr:gt(0)").remove();
                $.get("/applets/product/page", {
                    "pname": pname,
                    "pageNo": pageNo,
                    "pageSize": 5,
                    "inuse": "",
                    "order": order,
                    "ord": ord
                }, function (da) {
                    if (da.code != 0) {
                        alert(da.msg);
                        return;
                    }
                    var res = da.data;
                    total = res.total;
                    $.each(res.list, function (i, d) {
                        product.append("<tr><td>" + d.id +
                            "</td><td>" + d.name +
                            "</td><td>" + d.price +
                            "</td><td>" + d.typeid +
                            "</td><td>" + d.sku +
                            "</td><td>" + d.spu +
                            "</td><td>" + d.clicknums +
                            "</td><td>" + d.salenums +
                            "</td><td><a class='btn btn-primary btn-sm' href='/applets/product/goPurchase?id=" + d.id + "&userId=" + getUrlParam("userId") + "&price=" + d.price + "'>加入购物车</a>&nbsp;" +
                            "<button type='button' class='btn btn-primary btn-sm' code='" + d.id + "' id='shoucang'>收藏</button></td></tr>"
                        )
                    })
                })
            };

            var prodId = null;
            //删除事件
            product.on("click", '#shoucang', function () {
                prodId = $(this).attr("code");
                collection();
                return false;
            });

            //ajax删除
            function collection() {
                $.get("/Kshop/collect/collection", {"prodId": prodId, "userId": getUrlParam("userId")}, function (da) {
                    alert(da.msg);
                });
            };


            show();

            //销量升序
            $("#salenums").click(function () {
                ord = "salenums";
                pageNo = 1;
                show();
                return false;
            });
            //价格升序
            $("#priceup").click(function () {
                ord = "price";
                pageNo = 1;
                show();
                return false;
            });
            //价格降序
            $("#pricedw").click(function () {
                ord = "price";
                pageNo = 1;
                order = 0;
                show();
                return false;
            });

            getParam();

            function getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null)
                    return unescape(r[2]);
                return null; //返回参数值
            };


            function getParam() {
                userId = getUrlParam("userId");
                $("#adda").attr("href", "/kUser/UserMenu.html?userId=" + userId);
                $("#goCart").attr("href", "/KCart/ShowUserCart.html?userId=" + userId);
            };
        });
    </script>
</head>
<body>
<div class="container">
    <h2 class="text-center">商品展示</h2>

    <!--导航-->
    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" href="#" id="adda">返回用户菜单</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#" id="goCart">查看购物车</a>
        </li>
        <li class="nav-item">
            <button type="button" class="btn btn-link" id="salenums">销量升序</button>
        </li>
        <li class="nav-item">
            <button type="button" class="btn btn-link" id="priceup">价格升序</button>
        </li>
        <li class="nav-item">
            <button type="button" class="btn btn-link" id="pricedw">价格降序</button>
        </li>
    </ul>

    <!--查询表单-->
    <form action="#" method="post">
        <div class="form-group">
            <label>商品名称(模糊)</label>
            <input type="text" name="name" id="pname" class="form-control">
        </div>
        <div>
            <input type="button" value="搜索商品" class="form-control" id="button">
        </div>
    </form>
</div>

<div class="container">
    <table class="table table-striped" id="product">
        <thead>
        <tr>
            <th>ID</th>
            <th>商品名称</th>
            <th>价格</th>
            <th>分类</th>
            <th>规格</th>
            <th>参数</th>
            <th>浏览次数</th>
            <th>销量</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

<div class="container offset-md-5">
    <ul class="pagination">
        <li class="page-item" id="first"><a class="page-link">首页</a></li>
        <li class="page-item" id="prev"><a class="page-link">上一页</a></li>
        <li class="page-item" id="next"><a class="page-link">下一页</a></li>
        <li class="page-item" id="last"><a class="page-link">尾页</a></li>
    </ul>
</div>

</body>
</html>